package com.hm.cms.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.cms.auto.su.ControllerResult;
import com.hm.cms.auto.su.R;
import com.hm.cms.service.api.ArticleApiService;
import com.hm.cms.vo.api.ApiArticleVo;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.su.bean.ServerResponse;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author shishun.wang
 * @date 下午3:42:01 2017年10月13日
 * @version 1.0
 * @describe 内容管理
 */
@ResponseBody
@RestController
@RequestMapping(R.Restful.API + "/article")
public class ArticleApi {

	@Autowired
	private ArticleApiService articleApiService;

	private static final String SWWAGER_TAG = "内容管理";

	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页查询内容列表", httpMethod = "GET", notes = "分页查询内容列表")
	public ResponseEntity<ServerResponse<PageInfo<ApiArticleVo>>> query(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") int pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") int pageSize,
			@ApiParam(required = true, name = "isStatic", value = "已完成静态化") @RequestParam(required = true, name = "isStatic") boolean isStatic)
			throws Exception {
		return ControllerResult.success(articleApiService.query(new PageInfoParam(pageNumber, pageSize), isStatic));
	}

	@RequestMapping(value = "/{articleId}", method = RequestMethod.PATCH)
	@ApiOperation(tags = SWWAGER_TAG, value = "静态化指定文件", httpMethod = "PATCH", notes = "静态化指定文件,返回静态化结果")
	public ResponseEntity<ServerResponse<Boolean>> staticArticle(
			@ApiParam(required = true, name = "articleId", value = "目标静态化文件编号") @PathVariable("articleId") String articleId) {
		articleApiService.staticArticle(articleId);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/static/all", method = RequestMethod.PATCH)
	@ApiOperation(tags = SWWAGER_TAG, value = "数据库全量静态化文件", httpMethod = "PATCH", notes = "数据库全量静态化文件,异步任务执行")
	public ResponseEntity<ServerResponse<Boolean>> staticArticleAll(
			@ApiParam(required = true, name = "cover", value = "覆盖") @RequestParam(required = true, name = "cover") boolean cover) {
		articleApiService.staticArticleAll(cover);
		return ControllerResult.success(true);
	}
}
